<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 哈希解法 时间：O(n) 空间O(1)
        function twoSum(numbers, target) {
            // 先把值和索引对应存到map中，然后遍历的时候去map里面找target-value有没有
            let map = {}
            for (let i = 0; i < numbers.length; i++) {
                // obj键是数组值，obj值是索引
                map[numbers[i]] = i
            }
            for (let i = 0; i < numbers.length; i++) {
                let temp = target - numbers[i]
                // 必须加 i!==map[temp] 条件，不能用自己
                if (i !== map[temp] && (map[temp] || map[temp] === 0)) {
                    return [i, map[temp]]
                }
            }
        }
        // console.log(twoSum([2, 3, 4], 6));
        console.log(twoSum([2, 7, 11, 15], 9));
    </script>
</body>

</html>